var app = getApp();

Page({
  data: {
    latitude: 39.909088,
    longitude: 116.397643,
    markers: [],
  },

  //跳转导航
  go(e) {
    // console.log('e:', e);
    let that = this;
    let index = e.detail.markerId;
    // console.log('openLocation..', that.data.markers[index]);
    wx.openLocation({
      latitude: Number(that.data.markers[index].latitude),
      longitude: Number(that.data.markers[index].longitude),
      success(res) {
        console.log('success-res:', res);
      },
      fail(res) {
        console.log('fail-res:', res);
      },
    })
  },
  //获取地图数据
  getMapData() {
    const that = this;
    that.setData({
      markers: []
    })
    let params = {};
    // app.http.post('/gd-fishing/avoid/map/list', params).then((res) => {
    //   // console.log('http-res:', JSON.stringify(res));
    //   if (app.errorAlert(res)) {
    //     return;
    //   }
    //   let data = res.data;
    //   for (let i = 0; i < data.length; i++) {
    //     this.showMap(i, data[i].lat, data[i].lng, data[i].title);
    //   }
    // })
    // console.log('http-res:', JSON.stringify(res));
    let res = {
      "code": 1,
      "msg": "成功",
      "data": [{
        "lng": "121.949169",
        "lat": "30.887955",
        "title": "环湖西一路儿童乐园"
      }, {
        "lng": "121.236129",
        "lat": "31.720063",
        "title": "西沙国家湿地公园-听潮平台"
      }, {
        "lng": "121.397027",
        "lat": "31.225171",
        "title": "上海长风海洋世界"
      }],
      "remarks": null
    };
    let data = res.data;
    for (let i = 0; i < data.length; i++) {
      this.showMap(i, data[i].lat, data[i].lng, data[i].title);
    }
  },
  //显示地图
  showMap(id, lat, lng, title) {
    var _this = this
    var allMarkers = _this.data.markers;
    const marker = {
      id: id,
      latitude: lat,
      longitude: lng,
      callout: {
        // 点击marker展示title
        content: title
      }
    }
    allMarkers.push(marker)
    // console.log('allMarkers:', allMarkers)
    _this.setData({
      latitude: allMarkers[0].latitude,
      longitude: allMarkers[0].longitude,
      markers: allMarkers
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.showMap(0, "31.092434", "121.619485");
    // this.showMap(1, "31.089210", "121.618680");
    // this.showMap(2, "31.074595", "121.643229");
    this.getMapData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})